package com.eonmind.vip.mapper;

import com.eonmind.common.contract.data.base.BaseMongoMapper;
import com.eonmind.common.provider.entity.vip.Subscription;
import com.mongoplus.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;


@Slf4j
@Component
public class SubscriptionMapper extends BaseMongoMapper<Subscription> {

    public void saveAllSub(List<Subscription> list) {
        Boolean b = saveOrUpdateBatch(list);
    }



    public List<Subscription> getSubByIds(List<Long> ids) {
        List<Subscription> subscriptionList = getByIds(ids);
        return subscriptionList;
    }

    public List<Subscription> getActSubByPlanId$UserId(List<Long> ids, Long userId, String status) {
        QueryWrapper<Subscription> queryWrapper = createMgoQuery().eq(Subscription::getUserId, userId)
                .eq(Subscription::getStatus, status)
                .in(Subscription::getPlanId, ids);
        return list(queryWrapper);
    }

    public List<Subscription> findByStatusAndEndDateBefore(String status, Long now) {
        QueryWrapper<Subscription> queryWrapper = createMgoQuery().eq(Subscription::getStatus, status)
                .gt(Subscription::getEndDate, now);
        return list(queryWrapper);
    }

    public List<Subscription> findAllActiveSubsByUserId(String status, Long userId) {
        QueryWrapper<Subscription> queryWrapper = createMgoQuery().eq(Subscription::getUserId, userId)
                .eq(Subscription::getStatus, status);
        return list(queryWrapper);
    }

    public Boolean findIsHaveExpired(String status, Long userId) {
        QueryWrapper<Subscription> queryWrapper = createMgoQuery().eq(Subscription::getUserId, userId)
                .eq(Subscription::getStatus, status);
        return !list(queryWrapper).isEmpty();
    }
}
